package com.zqh.app.utils;

import com.alibaba.fastjson.JSON;
import com.zqh.app.entity.Count;
import com.zqh.app.manager.AppCountManager;
import com.zqh.app.manager.AppPushManager;
import com.zqh.app.manager.AppStatisticsManager;
import com.zqh.common.entity.Query;
import com.zqh.common.utils.CommonUtils;
import com.zqh.common.utils.DateUtils;
import com.zqh.common.utils.TimeUtils;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Map;

/**
 *
 * 类名称：TimerTask
 * 类描述：定时器任务
 * 创建人：hhf
 * 创建时间 2018-07-19 16:00:00
 * @version  V1.0
 *
 */
@Component
public class TimerTaskUtils {

    @Autowired
    private AppPushManager appPushManager;
    @Autowired
    private AppStatisticsManager appStatisticsManager;

    @Autowired
    private AppCountManager appCountManager;

    /**
     * 每天上午十点执行
     * @throws ParseException
     */
    @Scheduled(cron = "0 59 23 ? * *")
    synchronized public void task9() throws ParseException, InterruptedException {

    }


    /**
     * 定时推送
     * 每天下午三点执行 (单数天执行)
     * @throws ParseException
     */
    @Scheduled(cron = "0 00 15 ? * *")
    synchronized public void task1() throws ParseException {
        System.out.println("task1"+TimeUtils.getCurrentTimeStamp());
        if (DateUtils.getSingleOrDouble() == 0){
            if (!appPushManager.isPush("push1",TimeUtils.getTime())){
                System.out.println("task1-完美");
                return;
            }
            System.out.println("task1-start");
            appPushManager.undetectedTen();  //超过十天未测试   1
            appPushManager.offAndOn();        //断断续续    2
            appPushManager.continuousFifteen();   //连续15天测量   3
            appPushManager.newUser();          //新用户发送   4
        }
    }

    /**
     * 定时推送
     * 每天上午十点执行 (双数天执行)
     * @throws ParseException
     */
    @Scheduled(cron = "0 0 10 ? * *")
    synchronized public void task2() throws ParseException, InterruptedException {
        System.out.println("task3");
        if (DateUtils.getSingleOrDouble() != 0 ){
            if (!appPushManager.isPush("push2",TimeUtils.getTime())){
                System.out.println("task3-完美");
                return;
            }
            System.out.println("task3-start");
            appPushManager.lockSleepOne();    //缺少睡眠和一键测试
            appPushManager.lockSleep();       //缺少睡眠
            appPushManager.lockOne();         //缺少一键测试
        }
    }

    /**
     * 定时统计app数据
     * 每天上午一点点执行 (双数天执行)
     * @throws ParseException
     */
    @Scheduled(cron = "0 59 23 ? * *")
    synchronized public void sysAllAppData() throws ParseException, InterruptedException {
        System.out.println("app统计数据开始");
        appStatisticsManager.saveAllAppData();
    }

    /**
     * 定时统计app数据
     * 每天上午一点点执行 (双数天执行)
     * @throws ParseException
     */
    @Scheduled(cron = "0 59 23 ? * *")
    synchronized public void sysAllAppData1() throws ParseException, InterruptedException {
        Count count = appCountManager.mainCountTotal(null);
        count.setOrgId("1");
        String params = JSON.toJSONString(count);
        Map map = JSON.parseObject(params);
        Query form = new Query(map);
        appCountManager.saveStatistics(form);

        Count count1 = appCountManager.mainCountTotal(new Query(JSON.parseObject("{orgId:'2'}")));
        count1.setOrgId("2");
        String params1 = JSON.toJSONString(count1);
        Map map1 = JSON.parseObject(params1);
        Query form1 = new Query(map1);
        appCountManager.saveStatistics(form1);
    }
}